$(function () {
	$(".edit,#addCategory").on("click", function (ev) {
		var tg = ev.target, _this = $(this), categoryId = $("#categoryId");
		A.alertHtml($(".varsion-box").html(), function (layero) {
			if (tg.className == "edit") {
				var json = _this.attr("data-json").split(",");
				layero.find("input[name='CateName']").val(json[1]);
				layero.find(".select") && layero.find(".select option[value='"+json[2]+"']").attr("selected",true);
				layero.find("input[name='Sort']").val(json[2]);
				categoryId.val(json[0]);
			}

		}, function (index, layero) {
			var _CateName = layero.find("input[name='CateName']").val();
			var typeId = layero.find("select[name='original']").val();
			var sort = layero.find("input[name='Sort']").val();
			var original = layero.find(".varsion-alert").attr('data-type');
			var id = categoryId.val();
			var data = {
				_id: id,
				original:original,
				Sort:sort,
				CateName: _CateName
			}
			if(typeId > 0){
				data.typeId = typeId
			}	
			$.ajax({
				url: '/admin/categoryListSave',
				type: 'POST',
				data: data,
				success: function (data) {
					if (data.success) {
						A.closeAll();
						A.Msg(data.msg, function () {
							categoryId.val('');
						})
						location.reload()
					}
				}
			});
		});
	})
	$(".delete").on("click", function () {
		var _this = $(this);
		var categoryId = _this.attr("data-id");
		$.ajax({
			url: '/admin/categoryListDelete',
			type: 'GET',
			data: {
				id: categoryId
			},
			success: function (data) {
				if (data.success) {
					A.closeAll();
					A.Msg(data.msg)
					location.reload()
				}
			}
		});
	});
  $('#select_type').on('change',function(){
  	var value = $(this).val();
  	console.log(value)
  	 window.location.href= '/admin/categoryList/'+value
  })
	//$("#deleteAll").on("click",function(){
	//    $.ajax({
	//        url:'/admin/categoryListDeleteAll',
	//        type:'GET',
	//        data:{id:categoryId},
	//        success:function(data){
	//            if(data.success){
	//                A.closeAll();
	//                A.Msg(data.msg)
	//                location.reload()
	//            }
	//        }
	//    });
	//})
})